import json

import cx_Oracle
import requests


def ficus_put(url, token, title_rol, data_list):
    header = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token
    }
    body = []
    for data in data_list:
        data = list(data)
        for index in range(len(data)):
            if isinstance(data[index], cx_Oracle.LOB):
                data[index] = data[index].read()
                if isinstance(data[index], bytes):
                    data[index] = str(data[index])
        rol_dict = dict(zip(title_rol, data))
        body.append(rol_dict)
    # print(body)
    # print(json.dumps(body, ensure_ascii=False).encode('utf-8'))

    r = requests.post(url, data=json.dumps(body, ensure_ascii=False).encode('utf-8'), params=None,
                      timeout=5, headers=header)
    print(r.status_code, r.text)


if __name__ == '__main__':
    url = 'http://10.3.1.147:30078//sobeyficus/v1/restcrawl/S1/scu20201124/yfwla8e347186983'
    token = '052134b0-0f94-40d3-b9c5-ca0ca65c7070'
    title_rol = ['BJH', 'BM', 'YWBM', 'RXNJ', 'RXRQ', 'XSH', 'ZYH', 'ZYFXH', 'XZLXDM', 'BZ', 'BJRS', 'XQH', 'BJJC', 'BJLXM', 'ZTDM']
    data_list = [
        ('02205132', '02205132', None, '2002', '2002.9', '205', '080603', None, '01', None, 0, None, None, None, None),
        ('02205133', '02205133', None, '2002', '2002.9', '205', '080603', None, '01', None, 0, None, None, None, None),
        ('02205134', '02205134', None, '2002', '2002.9', '205', '080603', None, '01', None, 0, None, None, None, None),
        ('02205135', '02205135', None, '2002', '2002.9', '205', '080603', None, '01', None, 0, None, None, None, None),
        ('02205182', '02205182', None, '2002', '2002.9', '205', '071201', None, '01', None, 0, None, None, None, None)
    ]
    ficus_put(url, token, title_rol, data_list)